package com.qiuling.iot.controller;

import com.qiuling.iot.aop.annotation.MyLog;
import com.qiuling.iot.constants.Constant;
import com.qiuling.iot.entity.EquipmentInfo;
import com.qiuling.iot.service.EquipmentInfoService;
import com.qiuling.iot.utils.DataResult;
import com.qiuling.iot.utils.JwtTokenUtil;
import com.qiuling.iot.vo.req.EquipmentInfoAddReqVO;
import com.qiuling.iot.vo.req.EquipmentInfoChangeReqVO;
import com.qiuling.iot.vo.req.EquipmentInfoUpdateReqVO;
import com.qiuling.iot.vo.resp.EquipmentInfoRespNodeVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * @Author：zhangqiang
 * @Description：
 * @Date：Created in 21:58 2020/2/13
 * @Modified By：
 */
@RestController
@RequestMapping("api")
@Api(tags = "设备管理-设备管理", description = "设备管理相关接口")
public class EquipmentController {

    @Autowired
    private EquipmentInfoService equipmentInfoService;

    @GetMapping("equipments")
    @ApiOperation(value = "查询所有设备数据接口")
    @MyLog(title = "设备管理-设备管理", action = "查询所有设备数据接口")
    @RequiresPermissions("sys:equipment:list")
    public DataResult<List<EquipmentInfo>> getAllEquipment(HttpServletRequest request) {

        String accessToken = request.getHeader(Constant.ACCESS_TOKEN);
        String userId = JwtTokenUtil.getUserId(accessToken);
        DataResult result = DataResult.success();
        result.setData(equipmentInfoService.selectAll(userId));
        return result;
    }

    @GetMapping("equipment/tree")
    @ApiOperation(value = "设备树形列表结构")
    @MyLog(title = "设备管理-设备管理", action = "设备树形列表结构")
    @RequiresPermissions(value = {"sys:equipment:update", "sys:equipment:add"}, logical = Logical.OR)
    public DataResult<List<EquipmentInfoRespNodeVO>> getEquipmentTree(
            @RequestParam(required = false) String equipmentId, HttpServletRequest request) {

        String accessToken = request.getHeader(Constant.ACCESS_TOKEN);
        String userId = JwtTokenUtil.getUserId(accessToken);
        DataResult result = DataResult.success();
        result.setData(equipmentInfoService.EquipmentTreeList(equipmentId, userId));
        return result;
    }

    @PostMapping("equipment")
    @ApiOperation(value = "新增设备接口")
    @MyLog(title = "设备管理-设备管理", action = "新增设备接口")
    @RequiresPermissions("sys:equipment:add")
    public DataResult<EquipmentInfo> addEquipment(@RequestBody @Valid EquipmentInfoAddReqVO vo,
                                                  HttpServletRequest request) {
        String accessToken = request.getHeader(Constant.ACCESS_TOKEN);
        String userId = JwtTokenUtil.getUserId(accessToken);
        DataResult result = DataResult.success();
        result.setData(equipmentInfoService.addEquipmentInfo(vo, userId));
        return result;
    }

    @PutMapping("equipment")
    @ApiOperation(value = "更新设备信息")
    @MyLog(title = "设备管理-设备管理", action = "更新设备信息")
    @RequiresPermissions("sys:equipment:update")
    public DataResult updateEquipment(@RequestBody @Valid EquipmentInfoUpdateReqVO vo) {
        equipmentInfoService.updateEquipmentInfo(vo);
        DataResult result = DataResult.success();
        return result;
    }

    @DeleteMapping("equipment/{id}")
    @ApiOperation(value = "删除设备接口")
    @MyLog(title = "设备管理-设备管理", action = "删除设备接口")
    @RequiresPermissions("sys:equipment:delete")
    public DataResult deleteEquipment(@PathVariable("id") String id) {
        equipmentInfoService.deleteEquipmentInfo(id);
        DataResult result = DataResult.success();
        return result;
    }

    @PutMapping("equipment/changeSwitch")
    @ApiOperation(value = "开启或关闭电机")
    @MyLog(title = "设备管理-设备管理", action = "开启或关闭电机")
    @RequiresPermissions("sys:equipment:update")
    public DataResult changeStatus(@RequestBody @Valid EquipmentInfoChangeReqVO vo, HttpServletRequest request) {
        String accessToken = request.getHeader(Constant.ACCESS_TOKEN);
        String userId = JwtTokenUtil.getUserId(accessToken);
        equipmentInfoService.changeStatus(vo, userId);
        DataResult result = DataResult.success();
        return result;
    }
}
